﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using CrystalDecisions.CrystalReports.Engine;
using LayerEntity.Sistema;
using CrystalDecisions.Shared;
using System.Drawing.Imaging;

namespace System2012.Creditos.Reportes
{
    public partial class rptMovimientoCaja : DevExpress.XtraEditors.XtraForm
    {
        Form frm;
        /// <summary>
        /// 1: geenera el recibo de movimiento de caja
        /// 2: Genera el reporte de cronogrma de pagos
        /// 3:imprime la cuenta corriente del cliente
        /// </summary>
        int op;
        DataTable dtb = new DataTable();
        DataTable dtbReport;
        int row;
        ReportDocument rptDoc = new ReportDocument();
        public rptMovimientoCaja()
        {
            InitializeComponent();
            InitEvent();
        }
        public rptMovimientoCaja(int row,int op,DataTable dtb)
        {
            InitializeComponent();
            //InitEvent();
            this.op = op;
            this.row=row;
            this.dtb = dtb;
        if (!BGWListarData.IsBusy) BGWListarData.RunWorkerAsync();
        }
        private void rptMovimientoCaja_Load(object sender, EventArgs e)
        {
           
            //crystalReportViewer1.show
          //  this.reportViewer1.RefreshReport();
        }

        private void toExportar(ReportDocument oRpt,string Format)
        {
            SaveFileDialog File = new SaveFileDialog();
            ExportOptions exportOpts = new ExportOptions();
            DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
            exportOpts = oRpt.ExportOptions;

            switch (Format)
            {
                case "rpt":exportOpts.ExportFormatType = ExportFormatType.CrystalReport;
                    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
                    File.Filter = "Crystal Report RPT|*.rpt";
                    break;
                case "xls": exportOpts.ExportFormatType = ExportFormatType.Excel;
                    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
                    File.Filter = "Microft Excel File XLS|*.xls";
                    break;
                case "doc": exportOpts.ExportFormatType = ExportFormatType.WordForWindows;
                    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
                    File.Filter = "Microsoft Word XLS|*.xls";
                    break;
                case "rtf": exportOpts.ExportFormatType = ExportFormatType.EditableRTF;
                    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
                    File.Filter = "Formato de Textto enrequesido RTF|*.rtf";
                    break;
                case "xml": exportOpts.ExportFormatType = ExportFormatType.Xml;
                    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
                    File.Filter = "XML XML|*.xls";
                    break;
                case "pdf": exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
                    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
                    File.Filter = "Documento PDF|*.pdf";
                    break;
            }
            
            if (File.ShowDialog() == DialogResult.OK)
            {
                diskOpts.DiskFileName = File.FileName;
                exportOpts.DestinationOptions = diskOpts;
                oRpt.Export();
            }
        }
        private void btnExportPDF(object sender, ItemClickEventArgs e)
        {
            toExportar(rptDoc, "pdf");
        }
        private void btnExportRPT(object sender, ItemClickEventArgs e)
        {
            toExportar(rptDoc, "rpt");
        }
        private void btnExportDOC(object sender, ItemClickEventArgs e)
        {
            toExportar(rptDoc, "doc");
        }
        private void btnExportRTF(object sender, ItemClickEventArgs e)
        {
            toExportar(rptDoc, "rtf");
        }
        private void btnExportXML(object sender, ItemClickEventArgs e)
        {
            toExportar(rptDoc, "xml");
        }
        private void InitEvent()
        {
         /*   this.btnExportDoc.ItemClick += new ItemClickEventHandler(this.btnExportDOC);
            this.btnExportPdf.ItemClick += new ItemClickEventHandler(this.btnExportPDF);
            this.btnExportRpt.ItemClick += new ItemClickEventHandler(this.btnExportRPT);
            btnExportRtf.ItemClick += new ItemClickEventHandler(this.btnExportRTF);
            btnExportXml.ItemClick += new ItemClickEventHandler(this.btnExportXML);*/
        }
        private void btnExportXLS_ItemClick(object sender, ItemClickEventArgs e)
        {
            toExportar(rptDoc,"xls");
        }
        private void BGWListarData_DoWork(object sender, DoWorkEventArgs e)
        {
            this.BGWListarData.ReportProgress(30, "Load Clientes");
            ESistema obj = new ESistema();
            StringBuilder cadena = new StringBuilder();
            DataTable dtbBGW= new DataTable ();
            switch (op)
            {
                case 1:
                    cadena.AppendFormat("where ID_SUCURSAL_MOV={0}", int.Parse(dtb.Rows[row]["ID_SUCURSAL_MOV"].ToString()));
                    cadena.AppendFormat("and serie='{0}' ", dtb.Rows[row]["serie"].ToString());
                    cadena.AppendFormat("and fecha='{0}'", dtb.Rows[row]["fecha"].ToString());
                    cadena.AppendFormat("and nro_comprobante='{0}'", dtb.Rows[row]["nro_comprobante"].ToString());
                    cadena.AppendFormat("and id_tranzaccion_mov='{0}'", dtb.Rows[row]["id_tranzaccion_mov"].ToString());
                    cadena.AppendFormat("and id_documento_mov={0}", int.Parse(dtb.Rows[row]["id_documento_mov"].ToString()));
                    obj.Condicion = cadena.ToString();
                    dtbBGW = System2012.SystemUtils.RecordmanagerCaja.getMovPagos(obj);
                    break;
               case 2:
                    cadena.AppendFormat("where id_sucursal_credito={0}", int.Parse(dtb.Rows[row]["id_sucursal_credito"].ToString()));
                    cadena.AppendFormat(" and id_credito='{0}' ", dtb.Rows[row]["id_credito"].ToString());
                    obj.Condicion = cadena.ToString();
                    dtbBGW = System2012.SystemUtils.RecordManagerCreditos.getCronogramaPagos(obj);
                    break;
               case 3:
                    cadena.AppendFormat("where id_cliente='{0}'", dtb.Rows[row]["id_cliente"].ToString());
                    obj.Condicion = cadena.ToString();
                    dtbBGW = System2012.SystemUtils.RecordManagerCreditos.getCtasporCobrar(obj);
                   break;
            }
            e.Result = dtbBGW;
            this.BGWListarData.ReportProgress(100, "Load Complete");
        }

        private void BGWListarData_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        { 
            dtbReport =(DataTable) e.Result;
            switch (op)
            {
                case 1:
                    rptDoc.Load(@"D:\Templates\Plantillas\Caja\rptMovcaja.rpt"); break;
                case 2: 
                    rptDoc.Load(@"D:\Templates\Plantillas\Caja\rptCronogramaPagos.rpt"); break;
                case 3:
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dtbReport);
                    ds.WriteXml(@"D:\Templates\ConfigXML\rptCuentaCorriente.xml");
                    rptDoc.Load(@"D:\Templates\Plantillas\Creditos\rptCuentaCorriente.rpt"); break;
            }
            rptDoc.SetDataSource(dtbReport);

           // reportViewer1.ServerReport.ReportPath = rptDoc;
            crystalReportViewer1.ReportSource = rptDoc;
            crystalReportViewer1.Refresh();
        }

    }
}